import { request } from "@/utils/requst";
import { dateFormat, debounce } from "../../utils/util";
Page({
    async onLoad(e) {
        const {id,type} = e
        if (id) {
            const result:any = await request('GET', `/sendMail/getMailInfo/${id}`)
            this.setData({formData: result.data.data,pageType:type})
        }
    },
    data: {
        formData: {
            name: '',
            emailAddress: '',
            sendDate: '',
            content: '',
            isPublic: false
        },
        minDate: new Date().getTime() + 60 * 60 * 24 * 1000,
        datetimeHidden: true,
        formatter(type, value) {
            if (type === 'year') {
                return `${value}年`;
            }
            if (type === 'month') {
                return `${value}月`;
            }
            if (type === 'day') {
                return `${value}日`;
            }
            return value
        },
        ruleText: {
            name: '',
            emailAddress: '',
            sendDate: '',
            content: ''
        },
        pageType: ''
    },
    blueValidateMail(event) {
        const email = event.detail.value
        const re = /\S+@\S+\.\S+/;
        re.test(email);
    },
    confirmDate(e) {
        console.log(e.detail)
        const date = dateFormat(new Date(e.detail))
        const currentFormData = this.data.formData
        currentFormData.sendDate = date
        this.setData({
            formData: currentFormData
        })
        this.setData({
            datetimeHidden: true
        })
    },
    handleShowDate() {
        this.setData({
            datetimeHidden: false
        })
    },
    handleDateCancel() {
        this.setData({
            datetimeHidden: true
        })
    },
    handleDateConfirm(e) {
        this.setData({
            datetimeHidden: true
        })
    },
    validateNull(e) {
        const inputContent = e.detail.value
        const { label, attribute } = e.currentTarget.dataset
        const errorMessage = this.data.ruleText
        const currentFormData = this.data.formData
        currentFormData[attribute] = inputContent
        console.log(label, attribute)
        this.setData({
            formData: currentFormData
        })
        if (!this.data.formData[attribute]) {
            errorMessage[attribute] = `${label}不能为空`
        } else {
            errorMessage[attribute] = ``
        }
        // 邮箱校验
        if (attribute === 'emailAddress' && currentFormData.emailAddress !== '') {
            const emailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
            const result = emailRegex.test(currentFormData.emailAddress)
            errorMessage.emailAddress = result ? '' : '邮箱格式不正确'
        }

        this.setData({
            ruleText: errorMessage
        })
    },
    send: debounce( function(this){
        setTimeout(async () => {
            const formData = this.data.formData
        const result = Object.keys(formData).every(key => formData[key] !== '')
        // 邮箱校验
        const emailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
        const mialIsTrue = emailRegex.test(formData.emailAddress)
        if ( !(mialIsTrue && result) ) {
            return wx.showToast({title: '请输入完整',icon: 'error'})
        }
        const result1 = await request('POST','/sendMail',this.data.formData)
        if (result1.data.status) {
            wx.showToast({
                title: `${this.data.formData.id  ? '修改成功！': '寄信成功！'}`,
                icon: 'success'
            })
            setTimeout(() => {
                wx.navigateBack()
            },1000)
        }
        }, 500)
    },2000,true),
    onSelectChange(e){
        const currentFormData = this.data.formData
        currentFormData.isPublic = e.detail
        this.setData({
            formData: currentFormData
        })
    },
    back(){
        wx.navigateBack()
    }
})